// _template 已经被标记为 deprecated, 使用_index_template

GET _index_template


// 例子1
// 例子1: 清理模板
delete _index_template/template_default

// 例子1: 清理模板
delete _index_template/template_test

// 例子1: 新建一个默认模板
PUT _index_template/template_default
{
    "index_patterns": ["*"],
    "priority": 1,
    "version": 1,
    "template": {
        "settings": {
            "number_of_shards": 1, // 分片
            "number_of_replicas": 1 // 副本
        }
    }
}
// 例子1: 新建一个模板
PUT _index_template/template_test
{
    "index_patterns": ["test*"],
    "priority": 2,
    "version": 1,
    "template": {
        "settings": {
            "number_of_shards": 1,
            "number_of_replicas": 2
        },
        "mappings": {
            "date_detection": false, // 日期识别
            "numeric_detection": true // 数字书别
        }
    }
}

// 例子1: 索引一篇文档
PUT test_template/_doc/1
{
    "a": "11" // 由于设置了numeric_detection, 这里 a 的值类型会被设置为long
}

// 例子1: 查看索引信息
GET test_template


// 例子2
// 例子2: date模块
PUT _component_template/ct_date
{
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" }
      }
    }
  }
}

// 例子2: ip模块
PUT _component_template/ct_ip
{
    "template": {
        "mappings": {
            "properties": {
                "ip_address": {
                    "type": "ip"
                }
            }
        }
    }
}
// 例子2: 新建一个模板
PUT _index_template/log_nginx
{
  "index_patterns": ["log_nginx_*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "properties": {
        "host_name": { "type": "keyword" },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
      "log_nginx": { }
    }
  },
  "priority": 200, //  确保覆盖默认的模板
  "composed_of": ["ct_date", "ct_ip"],
  "version": 3,
  "_meta": {
    "description": "nginx logs"
  }
}


// 模板仿真: 模板1
PUT /_component_template/ct1
{
  "template": {
    "settings": {
      "index.number_of_shards": 2
    }
  }
}
// 模板仿真: 模板2
PUT /_component_template/ct2
{
  "template": {
    "settings": {
      "index.number_of_replicas": 0
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}
// 模板仿真: 仿真
POST /_index_template/_simulate
{
  "index_patterns": ["my*"],
  "template": {
    "settings" : {
        "index.number_of_shards" : 3
    }
  },
  "composed_of": ["ct1", "ct2"]
}